//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package net.mingsoft.mdiy.action.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import freemarker.core.ParseException;
import freemarker.template.MalformedTemplateNameException;
import freemarker.template.TemplateNotFoundException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.mingsoft.basic.util.BasicUtil;
import net.mingsoft.config.MSProperties;
import net.mingsoft.mdiy.action.BaseAction;
import net.mingsoft.mdiy.biz.IPageBiz;
import net.mingsoft.mdiy.entity.PageEntity;
import net.mingsoft.mdiy.util.ConfigUtil;
import net.mingsoft.mdiy.util.ParserUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

@Api(
        tags = {"前端-自定义模块接口"}
)
@Controller("webDiyPath")
@RequestMapping({"/mdiyPage"})
public class PageAction extends BaseAction {
    @Autowired
    private IPageBiz pageBiz;

    public PageAction() {
    }

    @ApiOperation("自定义页面")
    @ApiImplicitParams({@ApiImplicitParam(
            name = "diy",
            value = "请求地址",
            required = true,
            paramType = "path"
    )})
    @GetMapping({"/{diy}"})
    public void diy(@PathVariable("diy") String diy, HttpServletRequest req, HttpServletResponse resp) {
        String htmlDir = MSProperties.diy.htmlDir;
        Map<String, Object> map = BasicUtil.assemblyRequestMap();
        map.forEach((k, v) -> {
            if (k != null && v != null) {
                map.put(k, v.toString().replaceAll("('|\"|\\\\)", "\\\\$1"));
            }

        });
        Boolean shortSwitch = ConfigUtil.getBoolean("短链配置", "shortLinkSwitch");
        if (BasicUtil.getWebsiteApp() != null) {
            map.put("appDir", BasicUtil.getWebsiteApp().getAppDir());
            map.put("url", BasicUtil.getWebsiteApp().getAppHostUrl());
            map.put("appId", BasicUtil.getWebsiteApp().getAppId());
        } else if (shortSwitch) {
            map.put("url", BasicUtil.getUrl());
            map.put("appDir", "");
        } else {
            map.put("url", BasicUtil.getUrl());
            map.put("appDir", BasicUtil.getApp().getAppDir());
        }

        String contextPath = BasicUtil.getContextPath();
        if (StringUtils.isNotBlank(contextPath) && "/".equalsIgnoreCase(contextPath)) {
            contextPath = "";
        }

        map.putIfAbsent("contextPath", contextPath);
        map.put("isDo", false);
        map.put("html", htmlDir);
        PageEntity page = new PageEntity();
        page.setPageKey(diy);
        PageEntity _page = (PageEntity)this.pageBiz.getOne(new QueryWrapper(page), false);

        try {
            String content = ParserUtil.rendering(_page.getPagePath().replace(" ", ""), map);
            BasicUtil.outString(resp, content);
        } catch (TemplateNotFoundException var11) {
            var11.printStackTrace();
        } catch (MalformedTemplateNameException var12) {
            var12.printStackTrace();
        } catch (ParseException var13) {
            var13.printStackTrace();
        } catch (IOException var14) {
            var14.printStackTrace();
        }

    }
}
